<?php

namespace App\Common\Utils;

use EasyWeChat\MiniProgram\Application as MiniProgramApplication;
use EasyWeChat\OfficialAccount\Application as OfficialAccountApplication;
use GuzzleHttp\Client;

class EasyWechat
{
    /**
     * 获取 official account
     * @return OfficialAccountApplication
     */
    public static function getOfficialAccount(): OfficialAccountApplication
    {
        // 从 hyperf 那里获取 access token
        $acceccToken = json_decode((new Client(['base_uri' => env('SDJ_HYPERF_URI')]))->get('api/h5/v2/wechat/a-t')->getBody()->getContents(), true);

        /** @var OfficialAccountApplication  $app */
        $app = app('wechat.official_account');
        $app->access_token->setToken($acceccToken['access_token'], $acceccToken['expires_in'] ?? 7200);

        return $app;
    }

    /**
     * @return MiniProgramApplication
     */
    public static function getMiniProgram(): MiniProgramApplication
    {
        return app('wechat.mini_program');
    }
}
